//
//  BK_Confirm_Order_Goods_Cell.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/2/21.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_Confirm_Order_Goods_Cell.h"
#import "BK_OD_Color.h"
#import "BK_Confirm_Order_Common.h"
@implementation BK_Confirm_Order_Goods_Cell
{
    UIImageView *_imgV_YuShou_Icon;//预售
    UIImageView *_imgV_goodsIcon;
    UILabel     *_lab_goodsName;
    UILabel     *_lab_goodsMoney;//价格
    UILabel     *_lab_goodsInfo;//商品属性
    UILabel     *_lab_goodsCount;
    UIView      *_view_topLine;
    //7天无理由退货组合
    UILabel     *_lab_NoReason_ReturnSales;
    UIImageView *_imgV_NoReason_ReturnSales;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize{
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    /*
     * 商品图片
     */
    _imgV_goodsIcon = [[UIImageView alloc] initWithFrame:CGRectZero];
    _imgV_goodsIcon.contentMode = UIViewContentModeScaleAspectFit;
//    _imgV_goodsIcon.layer.borderColor = kOrderMediumGrey.CGColor;
//    _imgV_goodsIcon.layer.borderWidth = 0.5;
    [self.contentView addSubview:_imgV_goodsIcon];
    
    /*
     * 预售
     */
    _imgV_YuShou_Icon = [[UIImageView alloc] init];
    [_imgV_YuShou_Icon setContentMode:UIViewContentModeScaleAspectFit];
    _imgV_YuShou_Icon.image = [UIImage imageNamed:@"icon_time_nor"];
    [self.contentView addSubview:_imgV_YuShou_Icon];

    
    /*
     * 商品名称
     */
    _lab_goodsName = [[UILabel alloc] initWithFrame:CGRectZero];
    _lab_goodsName.backgroundColor = [UIColor clearColor];
    _lab_goodsName.textColor = kOrderBlack2;
    _lab_goodsName.font = UIFontCustom(14);
    [_lab_goodsName setLineBreakMode:NSLineBreakByWordWrapping];
    _lab_goodsName.numberOfLines = 2;
    _lab_goodsName.text = @" --- ";
    [self.contentView addSubview:_lab_goodsName];
    
    /*
     * 该商品支持(不支持)7天无理由退货
     */
    _lab_NoReason_ReturnSales=[[UILabel alloc]init];
    _lab_NoReason_ReturnSales.backgroundColor = [UIColor clearColor];
    _lab_NoReason_ReturnSales.textColor = kOrderLightGrey1;
    _lab_NoReason_ReturnSales.font = UIFontCustom(12);
    _lab_NoReason_ReturnSales.numberOfLines = 1;
    _lab_NoReason_ReturnSales.text=@"";
    [self.contentView addSubview:_lab_NoReason_ReturnSales];
    
    _imgV_NoReason_ReturnSales = [[UIImageView alloc] initWithFrame:CGRectZero];
    [self.contentView addSubview:_imgV_NoReason_ReturnSales];
    
    /*
     * 商品价格
     */
    _lab_goodsMoney = [[UILabel alloc] initWithFrame:CGRectZero];
    _lab_goodsMoney.backgroundColor = [UIColor clearColor];
    _lab_goodsMoney.textColor = kOrderPriceRed;
    _lab_goodsMoney.font = UIFontCustom(14);
    _lab_goodsMoney.numberOfLines = 1;
    [self.contentView addSubview:_lab_goodsMoney];
    
    /*
     * 商品属性描述
     */
    _lab_goodsInfo = [[UILabel alloc] initWithFrame:CGRectZero];
    _lab_goodsInfo.backgroundColor = [UIColor clearColor];
    _lab_goodsInfo.textColor = kOrderLightGrey1;
    _lab_goodsInfo.font = UIFontCustom(12);
    _lab_goodsInfo.numberOfLines = 1;
    [self.contentView addSubview:_lab_goodsInfo];
    
    /*
     * 商品数量
     */
    _lab_goodsCount = [[UILabel alloc] initWithFrame:CGRectZero];
    _lab_goodsCount.backgroundColor = [UIColor clearColor];
    _lab_goodsCount.textColor = kOrderLightGrey1;
    _lab_goodsCount.font = UIFontCustom(14);
    _lab_goodsCount.numberOfLines = 1;
    [self.contentView addSubview:_lab_goodsCount];
    
    /*
     * 顶部的线
     */
    _view_topLine = [[UIView alloc] init];
    _view_topLine.backgroundColor = kOrderLineGrey;
    [self.contentView addSubview:_view_topLine];
    
    
    
    
    [self makeConstraints];
}

- (void)makeConstraints {
    
    /*
     * 商品图片
     */
    [_imgV_goodsIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ApplicationBackGauge);
        make.top.mas_equalTo(12.5);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(60);
    }];
    
    
    /*
     * 商品名称
     */
    [_lab_goodsName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_imgV_goodsIcon.mas_right).offset(ApplicationBackGauge);
        make.top.equalTo(_imgV_goodsIcon.mas_top).offset(0.0f);
        make.right.mas_equalTo(-100);
    }];
    
    
    
    /*
     * 预售
     */
    [_imgV_YuShou_Icon mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_lab_goodsName.mas_left);
        make.top.mas_equalTo(_lab_goodsName.mas_top).offset(2.8f);
        make.height.mas_equalTo(14);
        make.width.mas_equalTo(132.0f/45.0f*14);
    }];
    

    
    /*
     * 商品属性描述
     */
    [_lab_goodsInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lab_goodsName);
        make.top.equalTo(_lab_goodsName.mas_bottom).offset(0);
        make.right.mas_equalTo(-50);
    }];

    /*
     * 该商品支持(不支持)7天无理由退货
     */
    [_imgV_NoReason_ReturnSales mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lab_goodsName.mas_left);
        make.top.equalTo(_lab_goodsInfo.mas_bottom).offset(8);
        make.height.mas_equalTo(15);
        make.width.mas_equalTo(20);
    }];
    [_lab_NoReason_ReturnSales mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_imgV_NoReason_ReturnSales.mas_right).offset(3);
        make.height.mas_equalTo(25);
        make.centerY.equalTo(_imgV_NoReason_ReturnSales.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-10);
    }];
    
    
    /*
     * 商品价格
     */
    [_lab_goodsMoney mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-ApplicationBackGauge);
        make.top.equalTo(_imgV_goodsIcon);
    }];
    
    
    /*
     * 商品数量
     */
    [_lab_goodsCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_lab_goodsMoney);
        make.top.equalTo(_lab_goodsMoney.mas_bottom).offset(10);
    }];
    
    /*
     * 顶部的线
     */
    [_view_topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo(0.5);
    }];
    
    
    
}

- (void)fillData:(id)data{
    
    BK_orders_dic_order_order_items_Model *goodsInfo = data;
    
    if (goodsInfo) {
        [_imgV_goodsIcon load:goodsInfo.goods_img placeholderImage:[UIImage imageNamed:@"BKOrderPlaceholder"]];
        _lab_goodsName.text = goodsInfo.goods_name;
        _lab_goodsMoney.text = [NSString stringWithFormat:@"￥%@",goodsInfo.member_price];
        _lab_goodsInfo.text = goodsInfo.sale_property;
//        _lab_goodsInfo.text=@"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy";
        _lab_goodsCount.text = [NSString stringWithFormat:@"x%@",goodsInfo.goods_amount];
        
        NSString * str_goodsMoney=[NSString stringWithFormat:@"￥%@",goodsInfo.member_price];
        //计算商品价格的宽度【商品价格和商品名称的距离7个像素】
        
        //商品价格宽度
        CGFloat  width_goodsMoney = [BK_Confirm_Order_Goods_Cell getWidthWithTitle:str_goodsMoney font:14];
        
        //计算【商品名称】行数
        NSInteger Num_Name;
        if ([BK_Confirm_Order_Goods_Cell getWidthWithTitle:goodsInfo.goods_name font:14]<ScreenWidth-width_goodsMoney-ApplicationBackGauge-ApplicationBackGauge-60-10) {
            Num_Name=1;
        }else{
            Num_Name=2;
        }
        
        /*
         * 预购判断
         */
        if ([[NSString stringWithFormat:@"%@",goodsInfo.thNormalType] isEqualToString:@"1"]) {
            _imgV_YuShou_Icon.hidden = NO;
            _lab_goodsName.text = [NSString stringWithFormat:@"             %@",goodsInfo.goods_name];            //物品详情介绍
        }else {
            _lab_goodsName.text = goodsInfo.goods_name;            //物品详情介绍
            _imgV_YuShou_Icon.hidden = YES;
        }
        
        /*
         * 更新【商品名称】布局   一行的高度是36
         */
        [_lab_goodsName mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_imgV_goodsIcon.mas_right).offset(15);
            make.top.equalTo(_imgV_goodsIcon.mas_top).offset(-1.0f);
            make.right.mas_equalTo(-width_goodsMoney-ApplicationBackGauge);
            make.height.mas_equalTo(18*Num_Name);
        }];
        
        /*
         * 判断是否有商品属性
         */
        if (!goodsInfo.sale_property||[goodsInfo.sale_property isEqualToString:@""]) {
            /*
             * 商品属性描述
             */
            [_lab_goodsInfo mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_lab_goodsName);
                make.top.equalTo(_lab_goodsName.mas_bottom).offset(0);
                make.right.mas_equalTo(-50);
                make.height.mas_equalTo(0);
            }];
    
        }else{
            /*
             * 商品属性描述
             */
            [_lab_goodsInfo mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_lab_goodsName);
                make.top.equalTo(_lab_goodsName.mas_bottom).offset(5);
                make.right.mas_equalTo(-50);
                make.height.mas_equalTo(14);
            }];
        
        }
        
        
        if ([[NSString stringWithFormat:@"%@",goodsInfo.canReturnSaled] integerValue]==1) {
            _lab_NoReason_ReturnSales.text=@"支持7天无理由退货";//该商品支持【不支持】7天无理由退货
            _imgV_NoReason_ReturnSales.image=[UIImage imageNamed:@"BK_Order_icon_return"];
            [_imgV_NoReason_ReturnSales mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_lab_goodsName.mas_left);
                make.top.equalTo(_lab_goodsInfo.mas_bottom).offset(8);
                make.height.mas_equalTo(15);
                make.width.mas_equalTo(20);
            }];
        }else{
            _lab_NoReason_ReturnSales.text=@"不支持7天无理由退货";
            _imgV_NoReason_ReturnSales.image=[UIImage imageNamed:@"BK_Order_icon_return_no"];
            [_imgV_NoReason_ReturnSales mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_lab_goodsName.mas_left);
                make.top.equalTo(_lab_goodsInfo.mas_bottom).offset(8);
                make.height.mas_equalTo(15);
                make.width.mas_equalTo(15);
            }];
        }
        
        
    }
    
   
    
   
}

+ (CGFloat)computeHeight:(id)data{
    NSInteger  Type;//类型
    NSInteger Num_Name;//商品行数
    NSInteger Num_Sale_property;//商品属性
    
    
    
    BK_orders_dic_order_order_items_Model *goodsInfo = data;
    NSString * str_goodsMoney=[NSString stringWithFormat:@"￥%@",goodsInfo.member_price];
    //商品价格宽度
    CGFloat  width_goodsMoney = [BK_Confirm_Order_Goods_Cell getWidthWithTitle:str_goodsMoney font:14];
    //计算【商品名称】行数
    
    if ([BK_Confirm_Order_Goods_Cell getWidthWithTitle:goodsInfo.goods_name font:14]<ScreenWidth-width_goodsMoney-ApplicationBackGauge-ApplicationBackGauge-60-15) {
        Num_Name=1;
    }else{
        Num_Name=2;
    }
    
    if (!goodsInfo.sale_property||[goodsInfo.sale_property isEqualToString:@""]) {
        Num_Sale_property=0;
    }else{
        Num_Sale_property=1;
    }
    
    
    if (Num_Name==1&&Num_Sale_property==0) {
        Type=1;
    }else if(Num_Name==2&&Num_Sale_property==0) {
        Type=2;
    }else if(Num_Name==1&&Num_Sale_property==1) {
        Type=3;
    }else if(Num_Name==2&&Num_Sale_property==1) {
        Type=4;
    }
    
    
    /*
     * 情况一    商品名称一行   +  商品属性（无） +  7天  = 170/2=85
     */
    
    /*
     * 情况二    商品名称二行   +  商品属性（无） +  7天  = 170/2=85
     */
    
    /*
     * 情况三    商品名称一行   +  商品属性（1行） +  7天  = 174/2=87
     */
    
    /*
     * 情况三    商品名称二行   +  商品属性（1行） +  7天  = 210/2=105
     */
    
    if (Type==1) {
        return 85;
    }else if(Type==2){
        return 85;
    }else if(Type==3){
        return 87;
    }else if(Type==4){
        return 105;
    }
    return 85;
}
/**
 *   获取字符宽度
 */
+ (CGFloat)getWidthWithTitle:(NSString *)title font:(CGFloat )font {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 0)];
    label.text = title;
    label.font = [UIFont systemFontOfSize:font];
    [label sizeToFit];
    return label.frame.size.width;
}
@end


